package com.aischool.service;

import java.util.ArrayList;
import java.util.List;

import com.aischool.common.model.AiArticle;
import com.aischool.params.AiArticleParams;
import com.aischool.utils.RegexUtil;
import com.aischool.utils.StateEnum;
import com.aischool.utils.StringUtil;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

public class AiArticleService {

	/**
	 * 获取分页数据
	 * @param params
	 * @return
	 */
	public Page<Record> getPage(AiArticleParams params){
		String sql_select = "SELECT a.article_id, a.article_id, b.type_id, a.cat_id, a.article_title, a.list_pic_url, a.add_time, a.article_state, b.cat_name ";
		String sql_from   =	"FROM ai_article a " + 
							"INNER JOIN ai_article_cat b ON b.cat_id = a.cat_id ";
		String sql_where  =	"WHERE a.article_state >=0 AND b.link_course = 0 ";
		String sql_order = "ORDER BY a.article_id DESC ";
		
		//System.err.println(JSONObject.toJSONString(params));
		
		int idx = 0;
		List<Object> listObj = new ArrayList<Object>();
		if (params.getState() != StateEnum.ALL.getState()) {
			sql_where = "WHERE a.article_state = ? ";
			listObj.add(params.getState());
		}
		
		if (params.getTypeId()>0) {
			sql_where += " AND b.type_id = ? ";
			listObj.add(params.getTypeId());
		}
		
		if (params.getCatId()>0) {
			sql_from += "INNER JOIN (SELECT cat_id FROM ai_article_cat WHERE cat_id = ? OR parent_id = ?) AS cc " + 
					"		ON cc.cat_id = a.cat_id ";
			listObj.add(idx, params.getCatId());
			idx++;
			listObj.add(idx, params.getCatId());
			idx++;
		}
		
		if (params.getOrgId() > 0) {
			sql_from += "INNER JOIN (SELECT regexp_split_to_table(COALESCE(?,''),E',') AS ids) AS tt " + 
						"ON CAST(tt.ids AS INTEGER) = a.org_id " +
						"INNER JOIN (select cat_id from ai_app_article_cat WHERE org_id = ?) AS oac ON oac.cat_id = a.cat_id ";
			String oids = "0," + params.getOrgId();
			listObj.add(idx, oids);
			idx++;
			listObj.add(idx, params.getOrgId());
			idx++;
			
			sql_select += ", oau.use_state ";
			sql_from += "LEFT JOIN (select article_id, use_state, org_id FROM ai_app_article WHERE org_id = ?) AS oau ON oau.article_id = a.article_id ";
			listObj.add(idx, params.getOrgId());
			idx++;
			
			if (params.getUseState() != StateEnum.ALL.getState()) {
				if (params.getUseState() == 0) {
					sql_where += " AND oau.use_state IS NOT NULL ";
				}else if (params.getUseState() == 1){
					sql_where += " AND oau.use_state IS NULL ";
				}
				
			}
		}
		
		
		if (StrKit.notBlank(params.getKeywords())) {
			sql_where += " AND a.article_title LIKE ? ";
			listObj.add("%" + params.getKeywords() + "%");
		}
		return Db.paginate(params.getPageNo(), params.getPageSize(), sql_select, sql_from + sql_where + sql_order, listObj.toArray());
	}
	
	public AiArticle getInfo(int article_id) {
		Record rd = getOne(article_id);
		if (rd!=null) {
			return new AiArticle().put(rd);
		}
		return null;
	}
	
	public Record getOne(int article_id) {
		return Db.findFirst("SELECT a.*, b.cat_name "
				+ "FROM ai_article a INNER JOIN ai_article_cat b ON b.cat_id = a.cat_id "
				+ " WHERE a.article_id = ?", article_id);
	}
	
	public boolean del(int article_id) {
		int ir = Db.delete("DELETE FROM ai_article WHERE article_id = ?", article_id);
		return ir > 0;
	}
	
	public boolean delMore(String ids) {
		if (StrKit.isBlank(ids)) {
			return false;
		}
		if (RegexUtil.isSqlIds(ids)) {
			int ir = Db.delete("DELETE FROM ai_article WHERE article_id IN("+StringUtil.trim(ids, ',')+")");
			return ir > 0;
		}
		
		return false;
	}
}
